package com.able.concurrency.t1;

import java.util.concurrent.TimeUnit;

/**
 * @author abel.he
 * @data 2018/8/15 17:24
 * @describe synchronized : 锁对象。synchronized(this)和synchronized方法都是锁当前对象
 */
public class Test1  {

    private int count = 0;
    private Object o = new Object();

    public void synchObject() {
        synchronized (o) {
            System.out.println(Thread.currentThread().getName()
            + " count=" + count++);

        }
    }

    public void synchClass() {
        synchronized (this) {
            System.out.println(Thread.currentThread().getName() + " count=" + count++);
        }
    }

    public synchronized  void synchoClass1() {
        System.out.println(Thread.currentThread().getName() + " count=" + count++);

        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {

        final Test1 test1 = new Test1();
       new Thread(new Runnable() {
           public void run() {
               test1.synchoClass1();
           }
       }).start();

       new Thread(new Runnable() {
           public void run() {
               test1.synchoClass1();
           }
       }).start();
    }

}
